package com.kust.smarthome.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.smarthome.web.mapper.RateMapper;
import com.kust.smarthome.web.model.Rate;
import com.kust.smarthome.web.service.IRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-06
 */
@Service
public class RateServiceImpl extends ServiceImpl<RateMapper, Rate> implements IRateService {

    @Autowired
    private RateMapper rateMapper;


    @Override
    public Rate getRate(int userId, int personId) {
        QueryWrapper<Rate> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.eq("person_id", personId);
        return rateMapper.selectOne(query);
    }

    @Override
    public String[] getRateString(Integer userId, Integer personId) {

        QueryWrapper<Rate> query = new QueryWrapper<>();
        query.eq("user_id", userId)
                .eq("person_id", personId);
        Rate r = rateMapper.selectOne(query);
        String[] rate = r.getRate().split(";");
        //当获取到的数组长度大于70时,我们截取后70个数据并返回
        if (rate.length > 70)
            rate = Arrays.copyOfRange(rate, rate.length - 70,rate.length);

        return rate;
    }

    @Override
    public void insert(Rate rate) {
        rateMapper.insert(rate);
    }
}
